﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net;

namespace PSE.Framework.Communication.Configuration.Adapters
{
    public class NetworkCredentialConfiguration : CommunicationConfigurationElement
    {
        [ConfigurationProperty("domain", IsRequired = false)]
        public string Domain
        {
            get
            {
                return (string)this["domain"];
            }
        }

        [ConfigurationProperty("userName", IsRequired = true)]
        public string UserName
        {
            get
            {
                return (string)this["userName"];
            }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get
            {
                return PSE.Framework.Cryptography.RijndaelCryptographer.DecryptString((string)this["password"]);
            }
        }

        public ICredentials GetCredential()
        {
            if (!string.IsNullOrEmpty(this.Domain))
	        {
                return new NetworkCredential(this.UserName, this.Password, this.Domain);
	        }
            else if (!string.IsNullOrEmpty(this.UserName))
            {
                return new NetworkCredential(this.UserName, this.Password);
            }
            else
            {
                return null;
            }
        }
    }
}
